package com.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import java.util.UUID;

public class Util {
    public static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    public static int getIntProperties(Properties properties, String argument) {
        return Integer.parseInt((String) properties.get(argument));
    }

    public static long getLongProperties(Properties properties, String argument) {
        return Long.parseLong((String) properties.get(argument));
    }

    public static String getStrProperties(Properties properties, String argument) {
        return (String) properties.get(argument);
    }

    public static BufferedImage getBufferedImage() throws IOException {
        Random random = new Random();
        // 图片尺寸
        int width = 600;
        int height = 400;
        // 初始化图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置随机背景色
        g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g2d.fillRect(0, 0, width, height);
        g2d.dispose();

        // 尝试写入到ByteArrayOutputStream以测量大小
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (true) {
            // 写入图片到ByteArrayOutputStream
            ImageIO.write(image, "jpg", baos);
            byte[] imageBytes = baos.toByteArray();
            // 计算图片大小
            long sizeInKB = imageBytes.length / 1024;
            if (sizeInKB >= 57) {
                baos.close();
                return image;
            } else {
                // 如果没有达到，则继续增加图片元素
                addRandomElementsToImage(image, random);
                baos.reset(); // 重置ByteArrayOutputStream
            }
        }
    }
    public static void writePic(String location, String filename) throws IOException {
        BufferedImage bufferedImage = getBufferedImage();
        ImageIO.write(bufferedImage, "jpg", new File(location + "/" + filename));
    }

    private static void addRandomElementsToImage(BufferedImage image, Random random) {
        Graphics2D g2d = image.createGraphics();
        // 添加多个随机文本
        for (int i = 0; i < 5; i++) {
            g2d.setFont(new Font("Arial", Font.PLAIN, random.nextInt(20) + 10));
            g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            String randomText = "Random Text " + random.nextInt(1000);
            g2d.drawString(randomText, random.nextInt(image.getWidth()), random.nextInt(image.getHeight()));
        }

        // 绘制随机线条
        for (int i = 0; i < 20; i++) {
            g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            g2d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()), random.nextInt(image.getHeight()));
        }

        // 绘制随机形状
        for (int i = 0; i < 10; i++) {
            g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            int shapeType = random.nextInt(3);
            int x = random.nextInt(image.getWidth());
            int y = random.nextInt(image.getHeight());
            int size = random.nextInt(50);
            switch (shapeType) {
                case 0: // 圆形
                    g2d.fillOval(x, y, size, size);
                    break;
                case 1: // 矩形
                    g2d.fillRect(x, y, size, size);
                    break;
                case 2: // 椭圆形
                    g2d.fillOval(x, y, size, size);
                    break;
            }
        }
        g2d.dispose();
    }
}
